<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace Catalog\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Http\Header\RetryAfter;
use Zend\View\Model\ViewModel;
use Doctrine\ORM\EntityManager;
use Catalog\Service\ProductService;
use Utilities\Geolocation\GeolocationService;
use Catalog\Service\StoreService;
use Utilities\Session\SessionFacade;
use Catalog\Form\WallStickerForm;

/**
 */
class ProductController extends AbstractGeolocalizedController
{

    /**
     */
    protected $productService;

    /**
     *
     * @param ProductService $productService            
     * @param GeolocationService $geolocationService            
     * @param StoreService $storeService            
     */
    public function __construct(ProductService $productService, GeolocationService $geolocationService, StoreService $storeService)
    {
        parent::__construct($geolocationService, $storeService);
        
        $this->productService = $productService;
    }

    /**
     *
     * @return A view model describing the products
     *        
     */
    public function detailAction()
    {
        $sessionFacade = new SessionFacade();
        
        $store = parent::getStore();
        
        // Get the request parameters
        $productAlias = $this->params()->fromRoute('product_alias');
        $product = $this->productService->getProductByAliasAndLanguage($productAlias, $store->getLanguage());
        
        return new ViewModel(array(
            'texts' => $this->productService->getProductTexts($product->getId(), $store->getLanguage()),
            'images' => $this->productService->getProductImages($product->getId(), $store->getLanguage()),
            'locale' => $sessionFacade->getLocale(),
            'price' => $this->productService->getProductPrice($store->getId(), $product->getId()),
            'form' => $this->getProductForm($product->getForm(), $product->getId(), $store)
        ));
    }

    /**
     * 
     * @param string $form
     * @param int $productId
     * @param Store $store
     */
    private function getProductForm($form, $productId, $store)
    {
        return $this->getServiceLocator()->get('FormElementManager')->get($form, 
            array('options' => array(
                'product_id' => $productId,
                'product_service' => $this->productService,
                'store' => $store
            )
        ));
    }
}

?>